package com.lanou.tk.tourism.fragment;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.lanou.tk.tourism.R;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by dllo on 15/10/27.
 */
public class RecommendViewPager extends Fragment{
    private ImageView recommend_Vp_Iv;
    private String photo;//图片地址
    private String imgUrl;//图片点击的链接
    private RequestQueue queue;//定义请求队列


    //单例模式 整个应用中保证只有一个实例对象 且必须是通过静态方法获取
//    private static RecommendViewPagerFragment viewPagerFragment;
//
//    private RecommendViewPagerFragment() {
//
//    }

    public static RecommendViewPager getInstance(String photo, String imgUrl) {
        RecommendViewPager viewPagerFragment = new RecommendViewPager();

        //参数传递
        Bundle args = new Bundle();
        args.putString("photo", photo);
        args.putString("imgUrl", imgUrl);
        viewPagerFragment.setArguments(args);
        return viewPagerFragment;
    }

    public void onAttach(Activity activity) {
        super.onAttach(activity);
        //初始化请求队列
        queue = Volley.newRequestQueue(activity);

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            photo = bundle.getString("photo");
            imgUrl = bundle.getString("imgUrl");
        }
    }

    /**
     * 加载布局文件
     *
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragement_recommend_view_pager, null);

        recommend_Vp_Iv = (ImageView) v.findViewById(R.id.recommend_Vp_Iv);

        getBitmap(photo, recommend_Vp_Iv);


        return v;
    }

    public void getBitmap(String url, final ImageView imageView) {
        ImageRequest request = new ImageRequest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                //将返回的bitmap图片设置到图片组件中
                imageView.setImageBitmap(response);
            }

        },
                720, 200, Bitmap.Config.ARGB_8888,
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                }) {
            @Override
            public Map<String, String> getHeaders()
                    throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;


            }
        };
        queue.add(request);
    }


}
